package com.qifenqian.turbot.module.ui.order.orders

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseFragment
import com.qifenqian.turbot.module.adapter.order.OrderOrdersAdapter
import com.qifenqian.turbot.module.contract.OrderContract
import com.qifenqian.turbot.module.entity.response.order.OrderDetail
import com.qifenqian.turbot.module.entity.response.order.OrderListEntity
import com.qifenqian.turbot.module.presenter.OrderPresenter
import com.qifenqian.turbot.module.ui.dish.PARAM1
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.fragment_tab_order.view.*

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * @author H
 * @createDate 2019/1/10 14:03
 * @description 主页订单中订单tab中的订单列表
 * @see
 * @since
 */
private const val PARAM1 = "current_tab"

/**
 * @author H
 * @createDate 2019/1/10 16:43
 * @description 订单中的5个订单tab
 * @see
 * @since
 */
class TabOrderFragment : BaseFragment<OrderPresenter, OrderContract.View>(), OrderContract.View {

    companion object {
        @JvmStatic
        fun newInstance(currentTab: Int) =
            TabOrderFragment().apply {
                arguments = Bundle().apply {
                    putInt(PARAM1, currentTab)
                }
            }
    }

    override fun bindLayoutId(): Int {
        return R.layout.fragment_tab_order
    }

    override fun bindPresenter(): OrderPresenter? {
        return OrderPresenter(activity!!, this)
    }

    private var mCurrentTab: Int = 0   //根据当前页面来请求网络获取当前页面的订单list
    private var mPage: Int = 0   //根据当前页面来请求网络获取当前页面的订单list

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            mCurrentTab = it.getInt(PARAM1)
        }
    }

    private var mOrderOrdersAdapter: OrderOrdersAdapter? = null
    private var list: MutableList<OrderListEntity> = mutableListOf()

    override fun initial(view: View) {
        list.clear()
        for (i in 0..1) {
            val orderDetailList = mutableListOf<OrderDetail>()
            for (j in 0..1) {
                orderDetailList.add(OrderDetail("A1", 2, 4, "2018-11-16 12:10", "15562531563", 5, 500, 230, 270))
            }
            list.add(OrderListEntity("time$i", orderDetailList))
        }

        for (orderEntity in list) {
            for (order in orderEntity.orders) {
                orderEntity.addSubItem(order)
            }
        }

        mOrderOrdersAdapter = OrderOrdersAdapter(list)
        view.rv_tab_order.layoutManager = LinearLayoutManager(activity)
        view.rv_tab_order.adapter = mOrderOrdersAdapter
        mOrderOrdersAdapter!!.expandAll()
        mOrderOrdersAdapter!!.setOnChildClickListener(object : OrderOrdersAdapter.OnChildClickListener {
            override fun onClick(position: Int) {
                OrderDetailActivity.startThis(activity!!)
            }
        })

        view.srl_tab_order.setRefreshHeader(ClassicsHeader(activity))
        view.srl_tab_order.setRefreshFooter(ClassicsFooter(activity))
        view.srl_tab_order.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mPage++
            }
        })
        if (list.isNullOrEmpty()) {
            mOrderOrdersAdapter!!.setEmptyView(R.layout.layout_empty, view.rv_tab_order)
        }
    }

}
